/*
This file is part of Jumpers.

Jumpers is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Jumpers is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Jumpers.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.chaoticengine.cgll.entity.component.move;

import java.util.Map;
import org.chaoticengine.cgll.entity.component.Command;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;
import org.simpleframework.xml.Element;

public class TopDownMovementComponent extends BasicMovementComponent {
    /** Acceleration in pixels per second. */
    protected float a = 0.0f;

    /** Clipping rectangle to limit movement. */
    protected Rectangle clip = null;

    public TopDownMovementComponent() {
        super();

        commands.put("UP", new Command(this) {
            @Override
            public void doAction(int delta) {
                TopDownMovementComponent.this.accelerate(
                        new Vector2f(0.0f, -1.0f),
                        a / 1000.0f * delta);
            }
        });
        commands.put("DOWN", new Command(this) {
            @Override
            public void doAction(int delta) {
                TopDownMovementComponent.this.accelerate(
                        new Vector2f(0.0f, 1.0f),
                        a / 1000.0f * delta);
            }
        });
        commands.put("LEFT", new Command(this) {
            @Override
            public void doAction(int delta) {
                TopDownMovementComponent.this.accelerate(
                        new Vector2f(-1.0f, 0.0f),
                        a / 1000.0f * delta);
            }
        });
        commands.put("RIGHT", new Command(this) {
            @Override
            public void doAction(int delta) {
                TopDownMovementComponent.this.accelerate(
                        new Vector2f(1.0f, 0.0f),
                        a / 1000.0f * delta);
            }
        });
    }

    @Override
    public void update(GameContainer gc, StateBasedGame sb, int delta) {
        // Enabled commands are triggered, movement executed by BasicMovmentComponent
        super.update(gc, sb, delta);

        // Do clipping if applicable
        if (clip != null) {
            Vector2f pos = owner.getPosition();
            Rectangle bounds = owner.getBoundingRectangle();

            if (pos.getX() + bounds.getWidth() > clip.getMaxX()) {
                pos.set(clip.getMaxX() - bounds.getWidth(), pos.getY());
            } else if (pos.getX() < clip.getMinX()) {
                pos.set(clip.getMinX(), pos.getY());
            }

            if (pos.getY() + bounds.getHeight() > clip.getMaxY()) {
                pos.set(pos.getX(), clip.getMaxY() - bounds.getHeight());
            } else if (pos.getY() < clip.getMinY()) {
                pos.set(pos.getX(), clip.getMinY());
            }

            owner.setPosition(pos);
        }
    }

    @Override
    public Map<String, Command> getCommands() {
        return (commands);
    }

    @Element(name="accelleration")
    public float getAccelleration() {
        return(a);
    }

    @Element(name="accelleration")
    public void setAccelleration(float acc) {
        this.a = acc;
    }

    @Element(name="clip")
    public Rectangle getClip() {
        return(this.clip);
    }

    @Element(name="clip")
    public void setClip(Rectangle r) {
        this.clip = r;
    }
}